package com.enzo.pcms.login.controller;


import com.enzo.pcms.base.controller.BaseController;
import com.enzo.pcms.login.model.LoginForm;
import com.enzo.pcms.login.model.User;
import com.enzo.pcms.login.service.UserService;
import com.itool.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping(value="/user")
public class LoginController extends BaseController {
    private static final Logger logger=Logger.getLogger(LoginController.class);
    @Autowired
    private UserService userService;

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping(value="/viewLogin")
    public String viewLogin(){
        return "login/login";
    }

    /**
     * 登录
     * @param request
     * @param response
     * @param form
     * @return
     */
    @RequestMapping(value="/login")
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response,LoginForm form){
        ModelAndView mv=null;
        viewName="login/login";
        String msg="";
        try {
            String email=form.getEmail();
            String password=form.getPassword();
            User user=userService.getUserByEmail(email);
            if(user==null){
                msg="用户 "+email+"不存在";
            }else if(StringUtil.isNotBlank(password)&&password.equals(user.getPassword())){
                viewName="index/index";
                msg="欢迎用户: "+email;
            }else{
                msg="用户名或密码不正确";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.put("msg", msg);
        return getModelAndView();
    }



    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }


}